package com.icez.base_compose_framework.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

/**
 * 主题
 * @param darkTheme 是否为暗黑主题
 * @param dynamicColor 是否使用动态颜色
 * @param content 内容
 */
@Composable
public fun BTheme(darkTheme:Boolean = isSystemInDarkTheme(),dynamicColor:Boolean = false,content: @Composable () -> Unit) {
    val colorScheme = when{
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme){
                dynamicDarkColorScheme(context)
            }else{
                dynamicLightColorScheme(context)
            }
        }
        darkTheme -> darkColorScheme()
        else -> lightColorScheme()
    }
    val view = LocalView.current
    // 是否不是处于编辑状态
    if (!view.isInEditMode){
        SideEffect {
            //获取窗口对象
            val window = (view.context as Activity).window
            //设置窗口状态栏颜色
            window.statusBarColor = Color.Transparent.toArgb()
            //是否使用浅色主题（即文字和图标颜色）
            WindowCompat.getInsetsController(window,view).isAppearanceLightStatusBars = !darkTheme
        }
    }
    MaterialTheme(
        colorScheme = colorScheme,
        shapes = BShape.getBShape(),
        typography = BType.getBType(),
        content = content
    )
}